package com.net.xpay.partner.controller.partner;

import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.domain.stat.UserSafePayTransStat;
import com.net.xpay.common.manager.stat.UserSafePayTransStatManager;
import com.net.xpay.common.manager.stat.UserTransStatManager;
import com.net.xpay.common.model.UserUnionTransStatResultModel;
import com.net.xpay.common.model.search.UserUnionTransStatSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.partner.secutiry.RequestUser;
import com.net.xpay.partner.vo.UserUnionTransStatVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/partner/user-union-trans-stat")
@Api(tags = "合伙人/用户日转账统计", description = "用户日转账统计")
public class UserUnionTransStatController {

    @Autowired
    private UserTransStatManager userTransStatManager;

    @Autowired
    private UserSafePayTransStatManager userSafePayTransStatManager;

    @ApiOperation(value = "用户转账统计")
    @PostMapping(value = "/search")
    public ResultPagedList<UserUnionTransStatVo> search(@ApiIgnore @RequestUser Partner partner, @RequestBody UserUnionTransStatSearchModel searchModel) {
        searchModel.setPartnerId(partner.getId());
        // 根据查询条件进行查询
        List<UserUnionTransStatResultModel> records = userTransStatManager.searchUnion(searchModel);
        // 查询带条件后数据的条数
        long count = userTransStatManager.searchUnionCount(searchModel);

        List<UserUnionTransStatVo> vos = BeanCopierUtil.batchCopy(UserUnionTransStatResultModel.class, UserUnionTransStatVo.class, records);
        for (UserUnionTransStatVo vo : vos) {
            if (vo.getType() == 2) {
                UserSafePayTransStat userSafePayTransStat = userSafePayTransStatManager.getById(vo.getId());
                vo.setSuccessBankAmount(userSafePayTransStat.getSuccessBankAmount());
                vo.setSuccessBankCount(userSafePayTransStat.getSuccessBankCount());
                vo.setSuccessAlipayAmount(userSafePayTransStat.getSuccessAlipayAmount());
                vo.setSuccessAlipayCount(userSafePayTransStat.getSuccessAlipayCount());
            }
        }

        return new ResultPagedList<>(vos, count, searchModel);
    }


}
